package com.example.mvc;

import com.example.spring.task.User;

public class Demo3Dao {

    public String login(String username,String password){

        if (!(username.length()>=2&&username.length()<=5)){
            return "用户名格式不正确";
        }

        //查数据库
        try {
            Demo2Service demo2Service = new Demo2Service();
            User user = demo2Service.login(username);
            if (user==null){
                return "用户不存在";
            }

            if (password.equals(user.getPassword())){
                return "密码不正确";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "登录成功";

    }

    public String register(String username,String password,String newPassword)throws Exception{

        if (!(username.length()>=2&&username.length()<=5)) {

            return "用户格式不正确";

        }

        if (!password.equals(newPassword)){
            return "两次密码不一致";
        }

        Demo2Service demo2Service = new Demo2Service();


            User user = demo2Service.login(username);
            if (user!=null){
                return "用户已存在";
            }
            int i = demo2Service.register(username,password);
            if (i==0){
                return "注册失败";
            }

        return "注册成功";

    }
}
